package com.example.itcacp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.itcacp.entity.Project;
import com.example.itcacp.repository.mybatis.ProjectMapper;
import com.example.itcacp.service.ProjectService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements ProjectService {

    @Resource
    private ProjectMapper projectMapper;

    // 创建
    @Override
    public void createProject(Project project) {
        projectMapper.addProject (project);
    }

    @Override
    public List<Project> getAllProjects() {
        return projectMapper.selectProjectList();
    }

    /**
     * 分页查询
     * */
    @Override
    public PageInfo<Project> selectProjectsPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage ( pageNum, pageSize );
        List<Project> projectList =  projectMapper.selectProjectList();
        return PageInfo.of ( projectList );
    }

//    查询
    @Override
    public Project getProjectById(Long projectId) {
        return projectMapper.selectProjectById (projectId);
    }

    @Override
    public Project updateProject(Project project) {
        projectMapper.updateById(project);
        return project;
    }

    @Override
    public void deleteProject(Long projectId) {
        projectMapper.deleteById(projectId);
    }


    /**
     * 构建路径
     * */
    @Override
    public String createGitRepo(Long userId, String projectPath) throws IOException {
        // 创建用户个人仓库地址
        String userRepoPath = Paths.get(System.getProperty("user.dir"), "repos", userId.toString(), projectPath).toString();
        File repoDir = new File(userRepoPath);
        if (!repoDir.exists()) {
            if (!repoDir.mkdirs()) {
                throw new IOException("Failed to create directory: " + userRepoPath);
            }
        }
        return userRepoPath;
    }

    /**
     * 上传文件到仓库
     * */
    @Override
    public void uploadFileToGit(Long projectId, MultipartFile file) throws IOException, InterruptedException {
        Project project = projectMapper.selectProjectById(projectId);  // 查找项目
        if (project != null) {
            String repoPath = project.getPath ();  // 拿到仓库地址
            Path filePath = Paths.get(repoPath, file.getOriginalFilename());   // 拿到文件名，构建文件路径
            Files.write(filePath, file.getBytes());  // 文件写入
        } else {
            throw new IOException("Project not found with id: " + projectId);
        }
    }

    @Override
    public PageInfo<Project> selectProjectsPageByUser(Integer pageNum, Integer pageSize, Long userId) {
        PageHelper.startPage ( pageNum, pageSize );
        List<Project> projectList =  projectMapper.selectProjectByUserIdList(userId);
        return PageInfo.of ( projectList );
    }

    @Override
    public List<Project> selectProjectAllByUserId(Long userId) {
        return projectMapper.selectProjectByUserIdList ( userId );
    }
}
